﻿package com.tvein.sanguo.swc.action.role
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class RoleItemInfo extends MovieClip
	{
		public static const NAME:String = "Role-ItemInfo";

		private var sourceServer:String;
		private var effectVol:int = 100;
		private var overLay:OverLay;

		private var itemID:int;
		
		public function RoleItemInfo()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtNote.border = true;
			txtNote.borderColor = 0x333333;
			
			btnDrop.visible = false;
			btnUnload.visible = false;
			btnLoad.visible = false;
			btnUse.visible = false;
			
			btnBack.addEventListener(MouseEvent.CLICK, onClick);
			btnDrop.addEventListener(MouseEvent.CLICK, onClick);
			btnUnload.addEventListener(MouseEvent.CLICK, onClick);
			btnLoad.addEventListener(MouseEvent.CLICK, onClick);
			btnUse.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setSource":
					sourceServer = params.Address;
					break;
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setData":
					setData(params);
					break;
			}
		}

		private function setData(params:Object):void
		{
			if (params.Name == "GetRoleItemInfo")
			{
				itemID = params.Process[0].Item_ID;
				
				var strType:String = "";
				var strState:String = "";
				
				if (params.Process[0].Amount > 0)
				{
					switch(params.Process[0].Type)
					{
						case 0:
							strType = "--";
						
							break;
						case 1:
							strType = "兵器";
						
							if (params.Process[0].State == 1)
							{
								btnLoad.visible = true;
							}
							else
							{
								btnUnload.visible = true;
							}
						
							break;
						case 2:
							strType = "头冠";
						
							if (params.Process[0].State == 1)
							{
								btnLoad.visible = true;
							}
							else
							{
								btnUnload.visible = true;
							}
						
							break;
						case 3:
							strType = "衣甲";
						
							if (params.Process[0].State == 1)
							{
								btnLoad.visible = true;
							}
							else
							{
								btnUnload.visible = true;
							}
						
							break;
						case 4:
							strType = "坐骑";
						
							if (params.Process[0].State == 1)
							{
								btnLoad.visible = true;
							}
							else
							{
								btnUnload.visible = true;
							}
						
							break;
						case 5:
							strType = "书籍";
						
							if (params.Process[0].State == 1)
							{
								btnUse.visible = true;
							}
						
							break;
						case 6:
							strType = "药品";
						
							if (params.Process[0].State == 1)
							{
								btnUse.visible = true;
							}
						
							break;
						case 7:
							strType = "暗器";
						
							if (params.Process[0].State == 1)
							{
								btnLoad.visible = true;
							}
							else
							{
								btnUnload.visible = true;
							}
						
							break;
					}
					
					if (params.Process[0].State == 1)
					{
						btnDrop.visible = true;
					}
					else
					{
						strState = "装备中";
					}
				}
				
				txtName.text = params.Process[0].Name;
				txtType.text = strType;
				txtRank.text = params.Process[0].Rank;
				txtAmount.text = params.Process[0].Amount + params.Process[0].Unit;
				txtBurden.text = String(params.Process[0].Amount * params.Process[0].Burden);
				txtNote.text = params.Process[0].Note;
				txtState.text = strState;
				
				var imgageloader:Loader = new Loader();
				imgageloader.load(new URLRequest(sourceServer + "/SanGuo/item/" + params.Process[0].ImageID + ".png"));
				imgageloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onImageLoad);
			}
		}
		
		private function _onImageLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onImageLoad);
			
			evt.target.content.x = 38;
			evt.target.content.y = 78;
			
			this.addChild(evt.target.content);
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnBack.removeEventListener(MouseEvent.CLICK, onClick);
			btnDrop.removeEventListener(MouseEvent.CLICK, onClick);
			btnUnload.removeEventListener(MouseEvent.CLICK, onClick);
			btnLoad.removeEventListener(MouseEvent.CLICK, onClick);
			btnUse.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}

		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnBack":
					dispatchEvent(new SwcEvent(NAME, ["-showBox", ["roleItem"]]));
					break;
				case "btnDrop":
					btnDrop.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-item", ["itemDrop", itemID]]));
					break;
				case "btnUnload":
					btnUnload.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-item", ["itemUnload", itemID]]));
					break;
				case "btnLoad":
					btnLoad.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-item", ["itemLoad", itemID]]));
					break;
				case "btnUse":
					btnUse.visible = false;
					dispatchEvent(new SwcEvent(NAME, ["-item", ["itemUse", itemID]]));
					break;
			}
		}
	}
}